package com.ruoyi.web.controller.api.city;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.cust.domain.*;
import com.ruoyi.cust.qo.CityProjectQo;
import com.ruoyi.cust.service.*;
import com.ruoyi.system.domain.city.CityTrade;
import com.ruoyi.system.domain.city.CityTradeSecond;
import com.ruoyi.system.domain.city.vo.TradeOptionVo;
import com.ruoyi.system.domain.city.vo.TradeSecondOptionVo;
import com.ruoyi.system.service.city.ICityTradeSecondService;
import com.ruoyi.system.service.city.ICityTradeService;
import com.ruoyi.system.service.impl.SysDictDataServiceImpl;
import com.ruoyi.web.core.config.SearchRedisHelper;
import com.ruoyi.web.core.utils.UserRecentSearch;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

@Api("主页")
@RequestMapping({"/api/city/home"})
@RestController
/* loaded from: CityHomeController.class */ public class CityHomeController extends BaseController {
    @Autowired
    private ICityTagService         cityTagService;
    @Autowired
    private ICityModuleService      cityModuleService;
    @Autowired
    private ICityCarouselService    cityCarouselService;
    @Autowired
    private ICityProjectService     cityProjectService;
    @Autowired
    private ICityTradeService       cityTradeService;
    @Autowired
    private ICityTradeSecondService cityTradeSecondService;
    @Autowired
    private ICityApplyService       cityApplyService;
    @Autowired
    private ICityNoticeService      cityNoticeService;
    @Autowired
    private SearchRedisHelper       searchRedisHelper;
    @Autowired
    private ICityUserService        cityUserService;
    @Autowired
    private ICityInfoService        cityInfoService;

    @Autowired
    private SysDictDataServiceImpl dictDataService;

    @ApiOperation("修改资讯")
    @GetMapping({"/infoId/{infoId}"})
    public AjaxResult getInfoId(@PathVariable("infoId") Long infoId) {
        CityInfo cityInfo = this.cityInfoService.selectCityInfoByInfoId(infoId);
        cityInfo.setViewTime(cityInfo.getViewTime() + 1);
        return toAjax(this.cityInfoService.updateCityInfo(cityInfo));
    }

    @ApiOperation("查询资讯")
    @GetMapping({"/info/{infoId}"})
    public AjaxResult getInfo(@PathVariable("infoId") Long infoId) {
        return success(this.cityInfoService.selectCityInfoByInfoId(infoId));
    }

    @ApiOperation("查询模块")
    @GetMapping({"/module/{moduleId}"})
    public AjaxResult getModule(@PathVariable("moduleId") Long moduleId) {
        return success(this.cityModuleService.selectCityModuleByModuleId(moduleId));
    }

    @ApiOperation("查询滚动通知")
    @GetMapping({"/notice/{noticeId}"})
    public AjaxResult getNotice(@PathVariable("noticeId") Long noticeId) {
        return success(this.cityNoticeService.selectCityNoticeByNoticeId(noticeId));
    }

    @ApiOperation("查询轮播")
    @GetMapping({"/carousel/{carouselId}"})
    public AjaxResult getCarousel(@PathVariable("carouselId") Long carouselId) {
        return success(this.cityCarouselService.selectCityCarouselByCarouselId(carouselId));
    }

    @ApiOperation("查询资讯列表")
    @GetMapping({"/infoList"})
    public Map<String, Object> list(CityInfo cityInfo) {
        startPage();
        List<CityInfo>      list = this.cityInfoService.selectCityInfoList(cityInfo);
        Map<String, Object> map  = new HashMap<>();
        map.put("list", getDataTable(list));
        map.put("num", new BigDecimal(getDataTable(list).getTotal()).divide(new BigDecimal(10), 0, RoundingMode.UP));
        return map;
    }

    @ApiOperation("查询项目")
    @GetMapping({"/project/{projectId}"})
    public AjaxResult getProject(@PathVariable("projectId") Long projectId,@RequestParam(value="userId",required = false) Long userId) {
        Long user = userId==null? this.getUserId(): userId;
        return success(this.cityProjectService.selectAppProjectByProjectId(user,projectId));
    }

    @ApiOperation("查询滚动通知列表")
    @GetMapping({"/noticeList"})
    public List<CityNotice> noticeList(CityNotice cityNotice) {
        return this.cityNoticeService.selectCityNoticeList(cityNotice);
    }

    @ApiOperation("查询轮播列表")
    @GetMapping({"/carouselList"})
    public List<CityCarousel> carouselList(CityCarousel cityCarousel) {
        return this.cityCarouselService.selectCityCarouselList(cityCarousel);
    }

    @ApiOperation("查询模块列表")
    @GetMapping({"/moduleList"})
    public List<CityModule> cityModuleList(CityModule cityModule) {
        List<CityModule> list = this.cityModuleService.selectCityModuleList(cityModule);
        if (list.size() < 40) {
            return list;
        }
        return list.subList(0, 40);
    }

    @ApiOperation("查询标签列表")
    @GetMapping({"/tagList"})
    public List<CityTag> tagList(CityTag cityTag) {
        return this.cityTagService.selectCityTagList(cityTag);
    }

    @ApiOperation("翻页查询项目列表")
    @GetMapping({"/projectPage"})
    public Map<String, Object> projectPage(CityProjectQo cityProjectQo) {
        CityUser cityUserDB = this.cityUserService.selectCityUserByUserId(cityProjectQo.getUserId());
        UserRecentSearch userRecentSearch = new UserRecentSearch();
        if (cityUserDB != null) {
            userRecentSearch.setUserId(cityUserDB.getUserId());
            userRecentSearch.setUnionId(cityUserDB.getUserId());
        }
        this.searchRedisHelper.addRedisRecentSearch(userRecentSearch);
        startPage();
        CityProject cityProject = new CityProject();
        BeanUtils.copyProperties(cityProjectQo, cityProject);
        List<CityProject> list = this.cityProjectService.selectJoinList(cityProject);
        this.searchRedisHelper.addRedisHotSearch(list,userRecentSearch.getStoreType());
        Map<String, Object> map = new HashMap<>();
        map.put("list", getDataTable(list));
        map.put("num", new BigDecimal(getDataTable(list).getTotal()).divide(new BigDecimal(10), 0, RoundingMode.UP));
        return map;
    }

    @ApiOperation("查询热门项目")
    @GetMapping({"/listHotSearch"})
    public Set<CityProject> listHotSearch(@RequestParam("storeType") String storeType) {
        Set<CityProject> list = this.searchRedisHelper.listHotSearch(storeType);
        if(CollectionUtils.isEmpty(list)){
            startPage();
            List<CityProject> listDb = this.cityProjectService.listHotSearch(storeType);
            this.searchRedisHelper.addRedisHotSearch(listDb,storeType);
            return new HashSet<>(listDb);
        }
        return  list;
    }

    @ApiOperation("查询最新前20个项目列表")
    @GetMapping({"/projectList"})
    public List<CityProject> projectList(CityProject cityProject) {
        List<CityProject> list = this.cityProjectService.selectJoinList(cityProject);
        if (list.size() < 20) {
            return list;
        }
        return list.subList(0, 20);
    }

    @ApiOperation("查询交易详情")
    @GetMapping({"/tradeOptions"})
    public List<TradeOptionVo> tradeOptions(CityTrade cityTrade) {
        startPage();
        List<CityTrade>     list              = this.cityTradeService.selectCityTradeList(cityTrade);
        List<TradeOptionVo> tradeOptionVoList = new ArrayList<>();
        if (!list.isEmpty()) {
            for (CityTrade c : list) {
                TradeOptionVo tradeOptionVo = new TradeOptionVo();
                tradeOptionVo.setValue(c.getTradeName());
                tradeOptionVo.setText(c.getTradeName());
                CityTradeSecond cityTradeSecond = new CityTradeSecond();
                cityTradeSecond.setTradeId(c.getTradeId());
                List<CityTradeSecond>     cityTradeSecondList     = this.cityTradeSecondService.selectCityTradeSecondList(cityTradeSecond);
                List<TradeSecondOptionVo> tradeSecondOptionVoList = new ArrayList<>();
                if (!cityTradeSecondList.isEmpty()) {
                    for (CityTradeSecond s : cityTradeSecondList) {
                        TradeSecondOptionVo tradeSecondOptionVo = new TradeSecondOptionVo();
                        tradeSecondOptionVo.setValue(s.getTradeSecondName());
                        tradeSecondOptionVo.setText(s.getTradeSecondName());
                        tradeSecondOptionVoList.add(tradeSecondOptionVo);
                    }
                }
                tradeOptionVo.setChildren(tradeSecondOptionVoList);
                tradeOptionVoList.add(tradeOptionVo);
            }
        }
        return tradeOptionVoList;
    }

    @ApiOperation("合作申请")
//    @Log(title = "合作申请", businessType = BusinessType.INSERT)
    @PostMapping({"/addCityApply"})
    public AjaxResult addCityApply(@RequestBody CityApply cityApply) {
        cityApply.setApplyId(Long.valueOf(IdUtil.getSnowflake(1, 1).nextId()));
        int i = this.cityApplyService.insertCityApply(cityApply);
        return toAjax(i);
    }

    @ApiOperation("编辑项目")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @GetMapping({"/editCityProject"})
    public AjaxResult edit(Long projectId, String text) {
        CityProject cityProject = this.cityProjectService.selectCityProjectByProjectId(projectId);
        if (cityProject != null) {
            cityProject.setProjectContent(text);
        }
        return toAjax(this.cityProjectService.updateCityProject(cityProject));
    }

    //没有写缓存
    @GetMapping("/dict")
    public TableDataInfo list(SysDictData dictData) {
        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }
}
